package com.wy.Service.impl;

import com.wy.Service.HuzuService;
import com.wy.Service.ParkingService;
import com.wy.dao.HuzuDao;
import com.wy.dao.ParkingDao;
import com.wy.dao.ResideDao;
import com.wy.pojo.Huzu;
import com.wy.pojo.Parking;
import com.wy.pojo.Reside;
import com.wy.vo.HuzuDto;
import com.wy.vo.PageVo;
import com.wy.vo.ParkingDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("parkingService")
@Transactional
public class ParkingrviceImpl extends BaseServiceImpl  implements ParkingService {

    @Resource
    private ParkingDao parkingDao;

    @Resource
    private ResideDao resideDao;

    @Override
    public PageVo searchAll(Integer current, Integer pageSize) {
        PageRequest pageRequest= PageRequest.of(current-1,pageSize);
        Page page= parkingDao.findAll(pageRequest);
        return new PageVo(page);
    }

    @Override
    public PageVo searchAll(Integer current, Integer pageSize, ParkingDto dto) {
        StringBuilder sbd=new StringBuilder("select r from Parking r  where 1=1 ");
        if(!StringUtils.isEmpty(dto.getName())){
            sbd.append(" and r.name like :name");
            dto.setName("%"+dto.getName()+"%");
        }
        if(!StringUtils.isEmpty(dto.getResideid())){
            sbd.append(" and r.resideid = :resideid");
            dto.setResideid(dto.getResideid());
        }

        return super.searchPagination(current,pageSize,sbd.toString(),dto);
    }

    @Override
    public List<Reside> searchReside() {
        return resideDao.findAll();
    }

    @Override
    public void add(Parking parking) {
        parkingDao.save(parking);
    }

    @Override
    public List<Parking> listAll() {
        return parkingDao.findAll();
    }

    @Override
    public void delete(Integer id) {
        parkingDao.deleteById(id);
    }

    @Override
    public void update(Parking parking) {
        parkingDao.save(parking);
    }

}
